{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/66_gradio.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Developing interactive web apps with gradio and leafmap**\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -U geemap gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import gradio as gr\n",
    "import geemap.foliumap as geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[21.79, 70.87], zoom=3)\n",
    "image = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 6000,\n",
    "    \"palette\": \"terrain\",\n",
    "}\n",
    "Map.addLayer(image, vis_params, \"SRTM\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "Visualize an Earth Engine layer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def viz_dem(vmin, vmax, palette):\n",
    "    Map = geemap.Map()\n",
    "    image = ee.Image(\"USGS/SRTMGL1_003\")\n",
    "    vis_params = {\n",
    "        \"min\": vmin,\n",
    "        \"max\": vmax,\n",
    "        \"palette\": palette,\n",
    "    }\n",
    "    Map.addLayer(image, vis_params, \"SRTM\")\n",
    "    return Map.to_gradio()\n",
    "\n",
    "\n",
    "vmin = gr.Number(value=0, label=\"Min value\")\n",
    "vmax = gr.Number(value=6000, label=\"Max value\")\n",
    "palette = gr.Textbox(value=\"terrain\", label=\"Palette\")\n",
    "title = \"Visualize Earth Engine Data\"\n",
    "demo = gr.Interface(viz_dem, [vmin, vmax, palette], \"html\", title=title)\n",
    "demo.launch()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "Visualize Earth Engine layers side by side."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def split(left, right):\n",
    "    Map = geemap.Map(center=(40, -100), zoom=4, height=600)\n",
    "\n",
    "    nlcd_left = ee.Image(f\"USGS/NLCD_RELEASES/2019_REL/NLCD/{left}\").select(\"landcover\")\n",
    "    nlcd_right = ee.Image(f\"USGS/NLCD_RELEASES/2019_REL/NLCD/{right}\").select(\n",
    "        \"landcover\"\n",
    "    )\n",
    "\n",
    "    left_layer = geemap.ee_tile_layer(nlcd_left, {}, f\"NLCD {left}\")\n",
    "    right_layer = geemap.ee_tile_layer(nlcd_right, {}, f\"NLCD {right}\")\n",
    "\n",
    "    Map.split_map(\n",
    "        left_layer,\n",
    "        right_layer,\n",
    "    )\n",
    "    return Map.to_gradio()\n",
    "\n",
    "\n",
    "left_input = gr.Textbox(value=\"2001\", label=\"Left Layer URL\")\n",
    "right_input = gr.Textbox(value=\"2019\", label=\"Right Layer URL\")\n",
    "\n",
    "title = \"Visualizing National Land Cover Database (NLCD)\"\n",
    "demo = gr.Interface(split, [left_input, right_input], \"html\", title=title)\n",
    "demo.launch()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "Visualize Cloud Optimized GeoTIFF (COG)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def split(left, right):\n",
    "    Map = geemap.Map(center=[21.79, 70.87], zoom=3)\n",
    "    Map.split_map(left, right)\n",
    "    print(Map.options[\"layersControl\"])\n",
    "    return Map.to_gradio()\n",
    "\n",
    "\n",
    "left_url = (\n",
    "    \"https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif\"\n",
    ")\n",
    "right_url = (\n",
    "    \"https://github.com/opengeos/data/releases/download/raster/Libya-2023-09-13.tif\"\n",
    ")\n",
    "left_input = gr.Textbox(value=left_url, label=\"Left Layer URL\")\n",
    "right_input = gr.Textbox(value=right_url, label=\"Right Layer URL\")\n",
    "title = \"Visualze Cloud Optimized GeoTIFF (COG)\"\n",
    "demo = gr.Interface(split, [left_input, right_input], \"html\", title=title)\n",
    "demo.launch()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
